import sys

from PyQt6.QtGui import QIcon, QPixmap
from PyQt6.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
                             QPushButton, QFileDialog, QLineEdit, QLabel, QTextEdit)
from PyQt6.QtCore import QThread, pyqtSignal, QDateTime, QByteArray, QBuffer, QIODevice

icon_data = "..."  # 省略了原始图标数据以节省空间


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('LINK114查询')
        self.setGeometry(100, 100, 750, 600)

        self.setup_icon()
        self.setup_ui()

    def setup_icon(self):
        icon_bytes = QByteArray.fromBase64(icon_data.encode('utf-8'))
        icon_buffer = QBuffer()
        icon_buffer.setData(icon_bytes)
        icon_buffer.open(QIODevice.OpenModeFlag.ReadOnly)
        pixmap = QPixmap()
        pixmap.loadFromData(icon_buffer.data())
        icon = QIcon(pixmap)
        self.setWindowIcon(icon)

    def setup_ui(self):
        layout = QVBoxLayout()

        self.file_path_label = QLabel('选择要查询的域名TXT:')
        layout.addWidget(self.file_path_label)

        self.file_path_input = QLineEdit()
        layout.addWidget(self.file_path_input)

        self.browse_button = QPushButton('浏览')
        self.browse_button.clicked.connect(self.browse_file)
        layout.addWidget(self.browse_button)

        self.start_button = QPushButton('开始')
        self.start_button.clicked.connect(self.start_scraping)
        layout.addWidget(self.start_button)

        self.console_output = QTextEdit()
        self.console_output.setReadOnly(True)
        layout.addWidget(self.console_output)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def browse_file(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择TXT文件", "", "TXT Files (*.txt)")
        if file_path:
            self.file_path_input.setText(file_path)
        else:
            self.file_path_input.setPlaceholderText("未选择文件")

    def log(self, message):
        timestamp = QDateTime.currentDateTime().toString()
        color = 'green' if "成功" in message else 'red'
        self.console_output.append(
            f'<span style="color:blue;">[{timestamp}]</span> <span style="color:{color};">{message}</span>')

    def start_scraping(self):
        pass  # 在这里实现具体的逻辑


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    main_window.log('<span style="color:black;">欢迎使用本软件.....</span>')
    main_window.log('<span style="color:black;">本软件用于收集站点友情链接.....</span>')

    sys.exit(app.exec())
